<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import resize from '@/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'future-line'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '280px'
    },
    chartInfo: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$el)
      const { mins, maxs, days } = this.chartInfo
      this.chart.setOption(
        {
          tooltip: {
            trigger: 'axis',
            backgroundColor: 'white',
            textStyle: {
              color: '#000'
            }
          },
          legend: {
            icon: 'roundRect',
            right: 10
          },
          grid: {
            left: '3%',
            right: '6%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [
            {
              name: '日',
              type: 'category',
              axisPointer: {
                type: 'shadow',
                shadowStyle: {
                  color: 'blue',
                  opacity: 0.05
                }
              },
              axisLine: {
                lineStyle: {
                  color: '#000'
                }
              },
              boundaryGap: false,
              data: days
            }
          ],
          yAxis: [
            {
              name: '温度℃',
              type: 'value',
              splitLine: {
                show: true,
                lineStyle: {
                  type: 'dashed'
                }
              },
              axisLine: {
                lineStyle: {
                  color: '#000'
                }
              }
            }
          ],
          series: [
            {
              id: 'y1',
              name: '最高温',
              type: 'line',
              smooth: true,
              color: 'red',
              // hoverAnimation:true,
              symbol: 'circle',
              areaStyle: {
                opacity: 0.3,
                color: {
                  type: 'linear',
                  x: 1,
                  y: 0,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: 'red'
                    },
                    {
                      offset: 1,
                      color: 'white'
                    }
                  ],
                  globalCoord: false
                }
              },
              data: maxs
            },
            {
              name: '最高温',
              type: 'effectScatter',
              coordinateSystem: 'cartesian2d',
              data: maxs, //2d坐标系
              symbol: 'circle',
              symbolSize: 10,
              showEffectOn: 'emphasis',
              rippleEffect: {
                brushType: 'stroke'
              },
              itemStyle: {
                normal: {
                  color: 'red',
                  borderColor: 'white'
                }
              },
              zlevel: 1,
              tooltip: {
                trigger: 'item',
                backgroundColor: 'white',
                textStyle: {
                  color: '#000'
                }
              }
            },
            {
              id: 'y0',
              name: '最低温',
              type: 'line',
              smooth: true,
              color: 'orange',
              symbol: 'circle',
              areaStyle: {
                opacity: 0.3,
                color: {
                  type: 'linear',
                  x: 1,
                  y: 0,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: 'orange' //0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: 'white' // 100% 处的颜色
                    }
                  ],
                  globalCoord: false // 缺省为 false
                }
              },
              data: mins
            },
            {
              name: '最低温',
              type: 'effectScatter',
              coordinateSystem: 'cartesian2d',
              data: mins, //2d坐标系
              symbolSize: 10,
              showEffectOn: 'emphasis',
              rippleEffect: {
                brushType: 'stroke'
              },
              itemStyle: {
                normal: {
                  color: 'orange',
                  borderColor: 'white'
                }
              },
              tooltip: {
                trigger: 'item',
                backgroundColor: 'white',
                textStyle: {
                  color: '#000'
                }
              },
              zlevel: 1
            }
          ]
        },
        true
      )
    }
  }
}
</script>
